﻿using System;



namespace FineCollection.Simplex
{

    /// <summary>
    /// A pair of same type members.
    /// </summary>
    /// <typeparam name="T">type of members.</typeparam>
    /// <seealso cref="Couple{TA,TB}"/>
    public struct Pair<T> : IEquatable<Pair<T>>
    {
        /// <summary>
        /// The first member of the pair.
        /// </summary>
        public readonly T A;

        /// <summary>
        /// The second member of the pair.
        /// </summary>
        public readonly T B;


        /// <summary>
        /// Trivial constructor.
        /// </summary>
        /// <param name="a">the first member.</param>
        /// <param name="b">the second member.</param>
        public Pair(T a, T b)
        {
            A = a;
            B = b;
        }


        /// <summary>
        /// Returns <i>i</i>-th member of the pair.
        /// </summary>
        /// <param name="index">1 for member <see cref="A"/>
        ///                     or 2 for member <see cref="B"/>.</param>
        /// <returns>a member.</returns>
        public T this [int index]
        {
            get
            {
                switch(index)
                {
                    case 1: return A;
                    case 2: return B;
                    default: throw new ArgumentOutOfRangeException("index", "Pair index can be only 1 or 2.");
                }
            }
        }


        public bool Equals(Pair<T> other)
        {
            return Equals(other.A, A) && Equals(other.B, B);
        }


        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (obj.GetType() != typeof (Pair<T>)) return false;
            return Equals((Pair<T>) obj);
        }


        public override int GetHashCode()
        {
            // ReSharper disable CompareNonConstrainedGenericWithNull
            return (A == null ? 0 : A.GetHashCode())
                 ^ (B == null ? 0 : B.GetHashCode());
            // ReSharper restore CompareNonConstrainedGenericWithNull
        }


        public static bool operator == (Pair<T> left, Pair<T> right)
        {
            return left.Equals(right);
        }


        public static bool operator != (Pair<T> left, Pair<T> right)
        {
            return !left.Equals(right);
        }


        public override string ToString()
        {
            return '(' + A.ToString() + ',' + B.ToString() + ')';
        }
    }


}